package pit.com.ordermobilesystem;

import java.util.ArrayList;

import pit.com.ordermobilesystem.http.OrderSystemHttpService;
import pit.com.ordermobilesystem.imageutil.ImageFetcher;
import pit.com.ordermobilesystem.model.Item;
import pit.com.ordermobilesystem.model.OrderItem;
import pit.com.ordermobilesystem.provider.OrderMobileProvider;
import pit.com.ordermobilesystem.sharepreference.OrderSharePreference;
import pit.com.ordermobilesystem.util.OrderConstant;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class DetailsFragment extends Fragment implements OrderConstant{
	/**
	 * chi tiết Fragment
	 */
	public Item item;
	private static int not = 1;
	private ImageFetcher mImageItem;
	private ImageView imbt_tang;
	private ImageView imbt_giam;
	private ImageView imv_donhang;
	private ImageView bt_chon;
	
	private TextView tv_soluong;
	private TextView  tv_giashow;
	
	private int tien ;
	private int gia;
	private int giamgia;
	// public static Order order;
	public static ArrayList<OrderItem> ListItem = new ArrayList<OrderItem>();
	
	public Item getItem() {
		return item;
	}
	public void setItem(Item itemnew) {
		item = itemnew;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.detailfragment, container,
				false);
		bt_chon = (ImageView) rootView.findViewById(R.id.bt_chon);
		imbt_tang = (ImageView) rootView.findViewById(R.id.ibt_tang);
		imbt_giam = (ImageView) rootView.findViewById(R.id.ibt_giam);
		imv_donhang =(ImageView)rootView.findViewById(R.id.iv_dathang);
		if (container != null) {
			setView(rootView);
			bt_chon.setVisibility(View.VISIBLE);
			imbt_tang.setVisibility(View.VISIBLE);
			imbt_giam.setVisibility(View.VISIBLE);
		}
		return rootView;
	}

	public void setView(View v) {

		String url = "";

		

		TextView tv_nameitem = (TextView) v.findViewById(R.id.tv_nameshow);
		TextView tv_description=(TextView)v.findViewById(R.id.tv_descriptionshow);
		ImageView img_dishitem = (ImageView) v.findViewById(R.id.img_dishitem);
		TextView tv_vatshow = (TextView) v.findViewById(R.id.tv_vatshow);
		tv_giashow = (TextView) v.findViewById(R.id.tv_giashow);
		tv_soluong = (TextView) v.findViewById(R.id.tv_soluong);

		mImageItem = new ImageFetcher(getActivity(), 100, 100);
		mImageItem.setLoadingImage(R.drawable.chickencurry);		
		if (!OrderSharePreference.getDownloadSync(getActivity())) {
			mImageItem.setOfflineMode(true);
		}
		
		// wv_dishitem.getSettings().setJavaScriptEnabled(true);
		// String customHtml = "<html><body><img src=" + url
		// + " width=250 height=300/></body></html>";
		// wv_dishitem.loadData(customHtml, "text/html", "UTF-8");
		 gia = (int) item.getPrice();
		 giamgia = (int)item.getDiscount();
		 tien = gia - giamgia;
		 

			url = OrderSystemHttpService.getImageProductURL(SERVER_HOST, PORT,item.getLinkImage()
					+"_large"+".jpg"); //+"_two"		
		
		tv_nameitem.setText(item.getNameItem());
		tv_description.setText(item.getDescription());

		mImageItem.loadImage(url, img_dishitem);
		not=1;
		tv_vatshow.setText("Giá trên đã bao gồm 10% VAT và 5% phí phục vụ");
		tv_giashow.setText(not+" x "+String.valueOf(tien)+ ".000 = "+tien*not+ ".000đ");
		tv_soluong.setText(String.valueOf(not));

		imv_donhang.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

					SelectedFragment Selected = new SelectedFragment();
					Selected.setItem(item);
					FragmentTransaction transaction = getFragmentManager()
							.beginTransaction();
					transaction.replace(R.id.details_frag, Selected);
					transaction.addToBackStack(null);
					transaction.commit();
			}
		});
		
		imbt_giam.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (not > 1) {
					not = not - 1;
				}
				tv_soluong.setText(String.valueOf(" "+not+" "));
				tv_giashow.setText(not+" x "+String.valueOf(tien)+ ".000 = "+tien*not+ ".000đ");
			}
		});
		
		imbt_tang.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				not = not + 1;
				tv_soluong.setText(String.valueOf(" "+not+" "));
				tv_giashow.setText(not+" x "+String.valueOf(tien)+ ".000 = "+tien*not+ ".000đ");
			}
		});
		
		bt_chon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// order = new Order();
				// xem xem trong list da co item nay chua
				int tile=0;
				for (int i = 0; i < ListItem.size(); i++) {
					OrderItem neworderitem = new OrderItem();
					neworderitem = ListItem.get(i);
					if (neworderitem.getIdItem() == item.getId()) {
						
						tile=1;
					}
					
				}
				if(tile==1){
					Toast.makeText(getActivity(), 
	                        "Món ăn đã có trong đơn hàng", Toast.LENGTH_LONG).show();
				}else{
					OrderItem orderitem = new OrderItem();
					orderitem.setIdItem(item.getId());
					orderitem.setNumberItem(not);
					orderitem.setPrice(item.getPrice());
					orderitem.setNameItem(item.getNameItem());
					orderitem.setDiscount(item.getDiscount());
					// getActivity().getContentResolver().insert(OrderMobileProvider.ORDER_ITEM_CONTENT_URI,
					// OrderMobileProvider.getContentValuesOrder_Item(orderitem));
					// them vao khi ma no chua co va so phai >=1

					ListItem.add(orderitem);
					Toast.makeText(getActivity(), 
	                        "Món ăn đã được thêm vào đơn hàng", Toast.LENGTH_LONG).show();
				}
				
				
				not = 1;				

			}
		});
	}

	@SuppressWarnings("unused")
	private OrderMobileProvider getContentResolver() {
		// TODO Auto-generated method stub
		return null;
	}

}
